package com.chenyue.cm.moudules.system.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.moudules.system.service.SysUsersService;
import com.chenyue.cm.moudules.system.utils.PasswordHelper;
import com.chenyue.cm.moudules.system.utils.ShiroUtils;
import com.chenyue.cm.system.dao.SysUsersMapper;
import com.chenyue.cm.system.domain.SysUsers;
import com.chenyue.cm.system.domain.SysUsersExample;
import com.chenyue.cm.utils.DateUtils;
import com.chenyue.cm.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author zhaojie
 * @Title: SysUsersServiceImpl
 * @ProjectName ROOT
 * @Description: TODO
 * @date 2018/11/1416:01
 */
@Service("sysUsersService")
public class SysUsersServiceImpl extends BaseServiceImpl<SysUsers> implements SysUsersService {
	
    @Autowired
    private SysUsersMapper sysUsersMapper;

    @Override
    @Resource(name = "sysUsersMapper")
    public void setBaseMapper(LcBaseMapper<SysUsers, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }
    
    
    
    @Override
   	@Resource(name = "sysUsersMapper")
   	public void setBaseDao(BaseMapper<SysUsers> baseDao) {
   		this.baseDao = baseDao;
   	}
	

    @Override
    public SysUsers selectByUsername(String username) {
        SysUsersExample example = new SysUsersExample();
        SysUsersExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<SysUsers> sysUsersList = this.sysUsersMapper.selectByExample(example);
        if (sysUsersList.size() > 0) {
            return sysUsersList.get(0);
        }
        return null;
    }

    @Override
    public Success updatePassWord(String password, String newPassWord) {
        try {
            SysUsers user = ShiroUtils.getUser();
            if (user == null){
                return Success.error("您未登录或登录超时，请您重新登录后再试");
            }
            SysUsers sysUsers = new SysUsers();
            sysUsers.setUsername(user.getUsername());
            sysUsers.setPassword(password);
            String pwd = PasswordHelper.encryptPassword(sysUsers);

            SysUsersExample example = new SysUsersExample();
            SysUsersExample.Criteria criteria = example.createCriteria();
            criteria.andUsernameEqualTo(user.getUsername());
            criteria.andPasswordEqualTo(pwd);
            List<SysUsers> usersList = this.sysUsersMapper.selectByExample(example);
            if (usersList.size() == 0) {
                return Success.error("您输入密码与账号不匹配，请重新输入！");
            }

            sysUsers.setId(user.getId());
            sysUsers.setPassword(newPassWord);
            String pword = PasswordHelper.encryptPassword(sysUsers);
            sysUsers.setPassword(pword);
            sysUsers.setUpdateTime(DateUtils.getTimeNow());
            this.getBaseMapper().updateById(sysUsers);
        } catch (Exception e) {
            LogUtil.error("密码修改异常", e, getClass());
        }
        return Success.success();
    }
}
